package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;


/**
 * 产地检疫申报--养殖端
 * @author niufei
 */
@Data
public class BreedReport implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -3022568554771910774L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     *  养殖场名称
     */
    private String breedFactoryName;

    /**
     * 栏舍
     */
    @JsonIgnore
    private String fromStableJson;

    /**
     * 栏舍
     */
    private List<Map<String,Object>> fromStables;

    /**
     * 是否是散户
     */
    private Integer isScale;

    /**
     * 养殖场地址
     */
    private String breedFactoryAddress;

    /**
     * 镇（养殖场所属区域--维护的数据）
     */
    private String breedFactoryTown;

    /**
     *
     */
    @JsonIgnore
    private String sourceJson;

    /**
     *
     */
    private List<Map<String , Object>> sources;

    /**
     * 申报号
     */
    private String number;

    /**
     * 申报日期
     */
    private Date date;

    /**
     * 申报人id
     */
    private Long userId;

    /**
     * 申报人名称
     */
    private String userName;

    /**
     * 申报人电话
     */
    private String userMobile;

    /**
     * 货主--收猪的人
     */
    private String owner;

    /**
     * 手机号码--收猪的人
     */
    private String ownerMobile;

    /**
     * 身份证号(收猪人的)
     */
    private String identityCard;

    /**
     * 申报点id
     */
    private Long applyReportId;

    /**
     * 申报点名称
     */
    private String applyReportName;

    /**
     * 发证点id
     */
    private Long certificateStationId;

    /**
     * 发证点名称
     */
    private String certificateStationName;

    /**
     * 养殖类型 1、猪 2、牛 3、羊 4、家禽（申报类型）
     */
    private Integer breedType;

    /**
     *
     */
    private Integer breedSecondType;

    /**
     *
     */
    private Integer breedThirdType;

    /**
     * 动物来源：10:家畜家禽;20:人工饲养;30:合法捕获
     */
    private Integer animalSource;

    /**
     * 野生动物驯养繁殖许可证号
     */
    private String wildAnimalBreedLicence;

    /**
     * 野生动物捕捉（猎捕）许可证号
     */
    private String wildAnimalCaptureLicence;

    /**
     *  用途 60:其他;10:饲养;20:屠宰;30:展览;40:演出;50:比赛
     */
    private Integer purposeType;

    /**
     * 状态，-1：不受理， -2：驳回，-3：不发证，0：未提交，1：待处理，2：已受理--待勘察，3、已勘察--待发证，4：已发证
     */
    private Integer status;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 发货日期(启运日期)
     */
    private Date deliverAt;

    /**
     * 发货地址（启运地址）
     */
    private String deliverAddress;

    /**
     * 屠宰场id
     */
    private Long butcherFactoryId;

    /**
     * 屠宰场名称
     */
    private String butcherFactoryName;

    private String receiveCode;

    /**
     * 收货地址(到底地址)
     */
    @JsonIgnore
    private String receiveAddress;
    /**
     * 收货日期(到达日期)
     */
    private Date receiveAt;

    /**
     * 收获地址（不存数据库）
     */
    private Map<String , Object> receiveAddressMap;

    /**
     * 是否留在本市 -1、外 1、本
     */
    private Integer isToLocal;

    /**
     *运输工具号牌
     */
    private String transportNumber;
    /**
     * 运输工具
     */
    private Integer transportation;
    /**
     * 运输工具 消毒方式
     */
    private String disinfection;

    /**
     * 消毒方式证明号
     */
    private String disinfectionNumber;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String earTagNumber;

    /**
     * 耳标号 不存数据库
     */
    private List<String> earTagNumbers;

    /**
     * 单据标志位 1：正常，-1：删除
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setReceiveAddress(String receiveAddress){
        this.receiveAddress = receiveAddress;

        if(Strings.isNullOrEmpty(receiveAddress)){
            this.receiveAddressMap = Maps.newHashMap();
        }else {
            try {
                this.receiveAddressMap = objectMapper.readValue(receiveAddress, new TypeReference<Map<String , Object>>() {});
            } catch (IOException e) {
                // this is fuck exception
            }
        }
    }

    public void setReceiveAddressMap(Map<String , Object> receiveAddressMap){
        this.receiveAddressMap = receiveAddressMap;

        if(receiveAddressMap == null || receiveAddressMap.isEmpty()){
            this.receiveAddress = null;
        }else {
            try {
                this.receiveAddress = objectMapper.writeValueAsString(receiveAddressMap);
            } catch (JsonProcessingException e) {
                // this is fuck exception
            }
        }
    }

    public void setEarTagNumber(String earTagNumber){
        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = Collections.emptyList();
        }else {
            try {
                this.earTagNumbers = objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                // this is fuck exception
            }
        }
    }


    public void setEarTagNumbers(List<String> earTagNumbers){
        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()){
            this.earTagNumber = null;
        }else {
            try {
                this.earTagNumber = objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                // this is fuck exception
            }
        }
    }

    public void setSourceJson(String sourceJson){
        this.sourceJson = sourceJson;

        if(Strings.isNullOrEmpty(sourceJson)){
            this.sources = new ArrayList<>();
        }else {
            try {
                this.sources = objectMapper.readValue(sourceJson, new TypeReference<List<Map<String , Object>>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setSources(List<Map<String , Object>> sources){
        this.sources = sources;

        if(sources == null || sources.isEmpty()){
            this.sourceJson = null;
        }else {
            try {
                this.sourceJson = objectMapper.writeValueAsString(sources);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

    public void setFromStables(List<Map<String,Object>> fromStables){
        this.fromStables = fromStables;

        if(fromStables == null || fromStables.isEmpty()){
            this.fromStableJson = null;
        }else {
            try {
                this.fromStableJson = objectMapper.writeValueAsString(fromStables);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

    public void setFromStableJson(String fromStableJson){
        this.fromStableJson = fromStableJson;

        if(Strings.isNullOrEmpty(fromStableJson)){
            this.fromStables = new ArrayList<>();
        }else {
            try {
                this.fromStables = objectMapper.readValue(fromStableJson, new TypeReference<List<Map<String,Object>>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }
}
